package jmine.tec.report.pdf;

import java.awt.Color;
import java.io.IOException;

import jmine.tec.report.api.ReportElement;
import jmine.tec.report.api.ReportTextBox;

import org.apache.commons.lang.StringUtils;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPTable;

/**
 * Renderer padrão de campos de texto para relatórios PDF.
 * 
 * @author Renan Kuroki
 */
public class ReportTextBoxPDFRenderer implements ReportElementPDFRenderer {

    private static final int FONT_SIZE = 9;

    private static final String DEFAULT_FONT_NAME = "Helvetica";

    public static final String WIDTH_PERCENTAGE = "widthPercentage";

    public static final String HORIZONTAL_ALIGNMENT = "horizontalAlignment";

    public static final int ALIGN_LEFT = Element.ALIGN_LEFT;

    public static final int ALIGN_CENTER = Element.ALIGN_CENTER;

    public static final int ALIGN_RIGHT = Element.ALIGN_RIGHT;

    /**
     * {@inheritDoc}
     */
    public String getRendererType() {
        return ReportTextBox.RENDERER;
    }

    /**
     * {@inheritDoc}
     */
    public void render(Document document, ReportElement element) throws IOException {
        this.doRender(document, (ReportTextBox) element);
    }

    /**
     * Modifica o documento pdf para adicionar o elemento
     * 
     * @param document document
     * @param element element
     * @throws IOException IOException
     */
    private void doRender(Document document, ReportTextBox element) throws IOException {
        try {
            this.generateTextBox(document, element);
        } catch (DocumentException e) {
            throw new IOException("Erro gerando documento PDF! " + e.getMessage());
        }
    }

    /**
     * {@inheritDoc}
     */
    public void generateTextBox(Document document, ReportTextBox textBox) throws DocumentException {
        PdfPTable table = new PdfPTable(1);
        Font font = FontFactory.getFont(DEFAULT_FONT_NAME, FONT_SIZE, Font.NORMAL, Color.BLACK);
        Font fontBold = FontFactory.getFont(DEFAULT_FONT_NAME, FONT_SIZE, Font.BOLD, Color.BLACK);

        Chunk chunkName = null;
        if (textBox.getTitle() == null || StringUtils.isBlank(textBox.getTitle())) {
            chunkName = new Chunk("", fontBold);
        } else {
            chunkName = new Chunk(String.format("%s \r\n", textBox.getTitle()), fontBold);
        }

        Chunk chunkValue = null;
        if (textBox.getBody() == null) {
            chunkValue = new Chunk("", font);
        } else {
            chunkValue = new Chunk(textBox.getBody(), font);
        }

        Phrase phrase = new Phrase(chunkName);
        phrase.add(chunkValue);

        Float widthPercentage = textBox.getParameters().getFloat(WIDTH_PERCENTAGE);
        if (widthPercentage != null) {
            table.setWidthPercentage(widthPercentage);
        }
        table.setHorizontalAlignment(textBox.getParameters().getInteger(HORIZONTAL_ALIGNMENT, ALIGN_LEFT));
        table.getDefaultCell().setNoWrap(false);
        table.getDefaultCell().setBorder(Rectangle.BOX);

        table.addCell(phrase);
        document.add(table);
        document.add(Chunk.NEWLINE);
    }
}
